package org.kojo.tdrm.servlet.ctrl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.kojo.tdrm.service.UserAppService;


public class FrontController extends HttpServlet {
	private static Logger log = Logger.getLogger(FrontController.class.getName());
	private Map commandMap=null;
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		commandMap = new HashMap<String, Class<BaseController>>();
		commandMap.put("/json/task", TaskAppController.class);
		commandMap.put("/json/user", UserAppController.class);
		commandMap.put("/json/notify", NotifyAppController.class);
		commandMap.put("/json/userconf", UserConfAppController.class);
		
	}
	
	protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		 // Create RequestContext based on request type
	    String pathInfo = request.getPathInfo();
	    String requestURI = request.getRequestURI();
	    if(null==commandMap.get(pathInfo)){
	    	response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"command not found");
	    	return;
	    }
	    Class klazz = (Class) commandMap.get(pathInfo);
	    BaseController controller = null;
	    try {
			controller = (BaseController)klazz.newInstance();
			controller.service(request, response);
		} catch (InstantiationException e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "cannot initiage class:"+klazz.getName());
		} catch (IllegalAccessException e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"illegal access class:"+klazz.getName());
		}
	    log.finer("klazz="+klazz.getName());
	    
	    log.finer("pathInfo="+pathInfo+",requestURI="+requestURI);
	    

	    // View Management - Navigate and Dispatch to appropriate view
	    //String page="/";
	    //distpatch(request,response,page);
	    jsonResponse(controller,response);
	}
	
	protected void distpatch(HttpServletRequest request,
			HttpServletResponse response, String page) throws ServletException, IOException {
		RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(page);
		dispatcher.forward(request, response);
	}
	protected void jsonResponse(BaseController ctrl,HttpServletResponse resp) throws IOException{
		if(ctrl.getError()!=null){
			resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ctrl.getError());
			return;
		}
		resp.getWriter().print(ctrl.getJsonResp());
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}

}
